package com.easemob.helpdesk.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Vibrator;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.easemob.helpdesk.activity.AlertDialog;
import com.easemob.helpdesk.activity.DispachList;
import com.easemob.helpdesk.activity.ServiceOrderDialog;
import com.easemob.helpdesk.recorder.MediaManager;
import com.housekeeperapp.MainApplication;
import com.housekeeperapp.R;

/**
 * Created by wsj on 2018/4/23.
 */

public class ServiceOderReceiver extends BroadcastReceiver {

    private static final String TAG = "ServiceOderReceiver";

    private SoundPool mSoundPlayer;
    private SoundPool timeoutSoundPlayer;
    private SoundPool remindSoundPlayer;

    private Vibrator vibrator;

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        String type = intent.getStringExtra("type");

        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        if (MediaManager.isPlaying() || TelephonyManager.CALL_STATE_OFFHOOK==telephonyManager.getCallState()||TelephonyManager.CALL_STATE_RINGING==telephonyManager.getCallState()){
            Log.d(TAG, "test media playing = onReceive: media playing is playing");
        }else {
            if ("serviceorder".equals(type)) {

                Intent dialog = new Intent(context, ServiceOrderDialog.class);
                dialog.setAction(Intent.ACTION_VIEW);
                dialog.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                dialog.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

                dialog.putExtra("extras", intent.getStringExtra("extras"));
                dialog.putExtra("message", intent.getStringExtra("message"));

                context.startActivity(dialog);

            } else if ("turnorder".equals(type)) {

                vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);

//            if(vibrator.hasVibrator()){
//                vibrator.cancel();
//            }

                if (MainApplication.getInstance().isNotiAlertVibrateStatus()) {
                    vibrator.cancel();
                    //震动
                    long[] pattern = new long[]{0, 180, 80, 120};
                    vibrator.vibrate(pattern, -1);
                }

                if (!MainApplication.getInstance().isNotiAlertSoundStatus()) {
                    return;
                }



                if (mSoundPlayer != null) {
                    mSoundPlayer.release();
                    mSoundPlayer = null;
                }

                mSoundPlayer = new SoundPool(10,
                        AudioManager.STREAM_MUSIC, 5);
                mSoundPlayer.load(context, R.raw.turn_order_sound, 1);
                mSoundPlayer.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
                    @Override
                    public void onLoadComplete(SoundPool soundPool, int sampleId,
                                               int status) {
                        mSoundPlayer.play(1, 1, 1, 0, 0, 1);
                    }
                });


            } else if ("timeoutorder".equals(type)) {

                vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
//            if(vibrator.hasVibrator()){
//                vibrator.cancel();
//            }

                if (MainApplication.getInstance().isNotiAlertVibrateStatus()) {
                    vibrator.cancel();
                    //震动
                    long[] pattern = new long[]{0, 180, 80, 120};
                    vibrator.vibrate(pattern, -1);
                }

                if (!MainApplication.getInstance().isNotiAlertSoundStatus()) {
                    return;
                }


                if (timeoutSoundPlayer != null) {
                    timeoutSoundPlayer.release();
                    timeoutSoundPlayer = null;
                }

                timeoutSoundPlayer = new SoundPool(10,
                        AudioManager.STREAM_MUSIC, 5);
                timeoutSoundPlayer.load(context, R.raw.timeout_order_sound, 1);
                timeoutSoundPlayer.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
                    @Override
                    public void onLoadComplete(SoundPool soundPool, int sampleId,
                                               int status) {
                        timeoutSoundPlayer.play(1, 1, 1, 0, 0, 1);
                    }
                });
            } else if ("remindorder".equals(type)) {
                vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);

//            if(vibrator.hasVibrator()){
//                vibrator.cancel();
//            }

                if (MainApplication.getInstance().isNotiAlertVibrateStatus()) {
                    vibrator.cancel();
                    //震动
                    long[] pattern = new long[]{0, 180, 80, 120};
                    vibrator.vibrate(pattern, -1);
                }

                if (!MainApplication.getInstance().isNotiAlertSoundStatus()) {
                    return;
                }

                if (remindSoundPlayer != null) {
                    remindSoundPlayer.release();
                    remindSoundPlayer = null;
                }

                remindSoundPlayer = new SoundPool(10,
                        AudioManager.STREAM_MUSIC, 5);
                remindSoundPlayer.load(context, R.raw.dispatch_order_sound, 1);
                remindSoundPlayer.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
                    @Override
                    public void onLoadComplete(SoundPool soundPool, int sampleId,
                                               int status) {
                        remindSoundPlayer.play(1, 1, 1, 0, 0, 1);
                    }
                });
            }
        }
    }
}
